var AUTHORIZATION_URL__MODULE_ID__ = "http://boredombuster.googlecode.com/svn/trunk/";
var MYRECIPES_URL__MODULE_ID__ = "http://mychef.foxban.com/gadget/recipe.php";
var SEND_URL__MODULE_ID__ = "http://mychef.foxban.com/send.php";
var CSS_URL__MODULE_ID__ = "http://boredombuster.googlecode.com/svn/trunk/css/";
var TRAY_IMAGE__MODULE_ID__ = getTrayImage__MODULE_ID__();

var prefs__MODULE_ID__ = new _IG_Prefs(__MODULE_ID__);
var tabs__MODULE_ID__ = new _IG_Tabs(__MODULE_ID__, "Busters");
var token__MODULE_ID__ = prefs__MODULE_ID__.getString("token");
var initialized__MODULE_ID__ = false;

var gNs__MODULE_ID__; // g namespace

var favOn__MODULE_ID__ = 'http://boredombuster.googlecode.com/svn/trunk/images/star_on.gif';
var favOff__MODULE_ID__ = 'http://boredombuster.googlecode.com/svn/trunk/images/star_off.gif';
var loading__MODULE_ID__ = 'http://boredombuster.googlecode.com/svn/trunk/images/loading.gif';

// URL of the recipe page last seen
var lastBusterUrl__MODULE_ID__ = "";

// Flag for my recipes tab updated
var myBustersUpdated__MODULE_ID__ = 1;

var tabTd__MODULE_ID__ = null; // object list of tab boxes



/**
 * Gets a string user preference as an array
 * @param {String} name The name of the preference
 * @return {Array.<String>} The preference as an array
 */
function getArrayPref__MODULE_ID__(name) {
  var prefStr = prefs__MODULE_ID__.getString(name);
  var prefArray = (prefStr == "") ? new Array() : JSON.parse(prefStr);
  return prefArray;
}



/**
 * Initiates gadget
 */
function initGadget__MODULE_ID__() {
  // Set Theme
  setTheme__MODULE_ID__();

  var busters_Id = tabs__MODULE_ID__.addTab("__MSG_tab_search__", "categories_id__mc__", setTabColors__MODULE_ID__);
  var favorite_Id = tabs__MODULE_ID__.addTab("__MSG_tab_favorites__", "favorites_id__mc__", fetchFavorites__MODULE_ID__);
  var myrecipes_Id = tabs__MODULE_ID__.addTab("__MSG_tab_my_recipes__", "mybusters_id__mc__", fetchMyBusters__MODULE_ID__);

  // Set first tab to be selected
  // Fixes bug in iGoogle gadget API
  tabs__MODULE_ID__.setSelectedTab(0);
  setToken__MODULE_ID__();
}



/**
 * Sets gadget theme
 */
function setTheme__MODULE_ID__() {
  var css_style = document.getElementById('css_style__mc__');
  var theme = prefs__MODULE_ID__.getString("themes");
  if (theme == "blue") {
    css_style.href = CSS_URL__MODULE_ID__ + "style_blue.css";
  } else if (theme == "ochre") {
    css_style.href = CSS_URL__MODULE_ID__ + "style_ochre.css";
  } else if (theme == "green") {
    css_style.href = CSS_URL__MODULE_ID__ + "style_green.css";
  } else if (theme == "apples") {
    css_style.href = CSS_URL__MODULE_ID__ + "style_apples.css";  
  } else if (theme == "summer") {
    css_style.href = CSS_URL__MODULE_ID__ + "style_summer.css";
  } else if (theme == "sea") {
    css_style.href = CSS_URL__MODULE_ID__ + "style_sea.css";
  } else if (theme == "pink") {
    css_style.href = CSS_URL__MODULE_ID__ + "style_pink.css";
  } else {
    // Default theme
    theme = blue;
    css_style.href = CSS_URL__MODULE_ID__ + "style_blue.css";
  }
  
  setTimeout(initTabColors__MODULE_ID__, 1000);
}

/**
 * Initializes tab colors
 * Only done at start of gadget
 */
function initTabColors__MODULE_ID__() {
  tabTd__MODULE_ID__ = document.getElementById("tl_" +  __MODULE_ID__ + "_header").firstChild.firstChild.getElementsByTagName("td");
  setTabColors__MODULE_ID__();
}

/**
 * Set the color of the gadget tabs
 */
function setTabColors__MODULE_ID__() {
  if (tabTd__MODULE_ID__ == null) {
    return;
  }

  try {
    for (var i = 0; i < tabTd__MODULE_ID__.length; i++) {
      if (tabTd__MODULE_ID__.item(i).className == ("tablib_selected tablib_selected" + __MODULE_ID__)) {
        tabTd__MODULE_ID__.item(i).id = "tab_selected__mc__";
      } else if (tabTd__MODULE_ID__.item(i).className == ("tablib_unselected tablib_unselected" + __MODULE_ID__)) {
        tabTd__MODULE_ID__.item(i).id = "tab_unselected__mc__";
      }
    }
  } catch(e) {
    setTimeout(initTabColors__MODULE_ID__, 1000);
  }
}

/**
 * Increases the width of the popup table in IE6
 * @param {Element} the popup table
 */
function IE6PopupFix__MODULE_ID__(popup_table) {
  if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.search("MSIE 6.0") != -1) {
    popup_table.style.marginLeft = "20px";
    popup_table.style.marginRight = "20px";
  }
}

_IG_RegisterOnloadHandler(initGadget__MODULE_ID__);
